<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
	<title>文章管理</title>
	<meta name="decorator" content="ani"/>
	<!-- editormd -->
	<%@include file="/webpage/include/editormd.jsp" %>
	<script type="text/javascript">
		var validateForm;
		var $table; // 父页面table表格id
		var $topIndex;//弹出窗口的 index
		var testEditor;
		function doSubmit(table, index){//回调函数，在编辑和保存动作时，供openDialog调用提交表单。
		  if(validateForm.form()){
			  $table = table;
			  $topIndex = index;
			  jp.loading();
			  $("#inputForm").submit();
			  return true;
		  }

		  return false;
		}

		function parseMdField(url){
			var fields = url.split('&');
			var md = 'editormd-markdown-doc', html = 'editormd-html-code';
			var mdField = 'contentsmd', htmlField = 'contents';
			var newUrl = '';
			for (var i = 0; i < fields.length; i++) {
				var param = fields[i];
				if (param.indexOf(md) > -1) {
					const split = param.split('=');
					param = mdField + '=' + split[1];
				} else if (param.indexOf(html) > -1) {
					const split = param.split('=');
					param = htmlField + '=' + split[1];
				}
				if (i === 0) {
					newUrl = param;
				} else {
					newUrl = newUrl + '&' + param;
				}
			}
			return newUrl;
		}


		$(document).ready(function() {
			validateForm = $("#inputForm").validate({
				submitHandler: function(form){
					var params = $('#inputForm').serialize();
					var _reqParam = parseMdField(params);
					jp.post("${ctx}/blog/note/save",_reqParam,function(data){
						if(data.success){
	                    	$table.bootstrapTable('refresh');
	                    	jp.success(data.msg);
	                    	jp.close($topIndex);//关闭dialog

	                    }else{
            	  			jp.error(data.msg);
	                    }
					})
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});

		$(function() {
			testEditor = editormd("editormd", {
				width   : "90%",
				height  : 400,
				syncScrolling : "single",
				saveHTMLToTextarea : true, //设置可保存为html 获取的时候testEditor.getHtml();就可以了
				/*theme : "dark",  //设置主题，有默认
				previewTheme : "dark",
				editorTheme : "pastel-on-dark",*/
				searchReplace : true,
				emoji : true,
				taskList : true,
				tocm            : true,         // Using [TOCM]
				tex : true,                   // 开启科学公式TeX语言支持，默认关闭
				flowChart : true,             // 开启流程图支持，默认关闭
				sequenceDiagram : true,       // 开启时序/序列图支持，默认关闭,
				dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
				imageUpload : true,
				imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
				imageUploadURL : "../../../blog/note/imageUpload",
				path    : "${pageContext.request.contextPath}/editor.md-master/lib/"
			});
		});
	</script>
</head>
<body class="bg-white">
		<form:form id="inputForm" modelAttribute="note" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>
		<table class="table table-bordered">
		   <tbody>
				<tr>
					<td class="width-15 active"><label class="pull-right">标题：</label></td>
					<td class="width-35">
						<form:input path="title" htmlEscape="false"  class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">分类：</label></td>
					<td class="width-35">
						<sys:treeselect id="category" name="category.id" value="${note.category.id}" labelName="category.name" labelValue="${note.category.name}"
										title="分类" url="/blog/category/treeData" extId="${category.id}" cssClass="form-control " allowClear="true"/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">编号：</label></td>
					<td class="width-35">
						<form:input path="code" htmlEscape="false"  class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">置顶：</label></td>
					<td class="width-35">
						<form:select path="top" class="form-control ">
							<form:options items="${fns:getDictList('top_yes_no')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
						<span class="help-inline">是否置顶到文章列表前</span>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">来源：</label></td>
					<td class="width-35">
						<form:select path="origin" class="form-control ">
							<form:options items="${fns:getDictList('ori_rep_trans')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
						<span class="help-inline">1:原创，2:转载，3:翻译</span>
					</td>
					<td class="width-15 active"><label class="pull-right">状态：</label></td>
					<td class="width-35">
						<form:select path="status" class="form-control ">
							<form:options items="${fns:getDictList('dra_text_recy')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
						<span class="help-inline">1:草稿，2:正文，3:回收站</span>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">缩略图地址：</label></td>
					<td class="width-35">
						<form:input path="thumUrl" htmlEscape="false" rows="4"    class="form-control "/>
					</td>
					<td class="width-15 active"><label class="pull-right">Banner图地址：</label></td>
					<td class="width-35">
						<form:input path="advertUrl" htmlEscape="false" rows="4"    class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">出行时间：</label></td>
					<td class="width-35">
						<form:input path="travelTime" htmlEscape="false"  class="form-control "/>
						<span class="help-inline">如：2018-11-20</span>
					</td>
					<td class="width-15 active"><label class="pull-right">出行天数：</label></td>
					<td class="width-35">
						<form:input path="travelDay" htmlEscape="false"  class="form-control "/>
						<span class="help-inline">如：2 | 3.5</span>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">人物：</label></td>
					<td class="width-35">
						<form:input path="personage" htmlEscape="false"  class="form-control "/>
						<span class="help-inline">如：家庭 | 5个人 | 公司 </span>
					</td>
					<td class="width-15 active"><label class="pull-right">人均费用：</label></td>
					<td class="width-35">
						<form:input path="percapita" htmlEscape="false"  class="form-control "/>
						<span class="help-inline">如：300 默认【RMB】</span>
					</td>
				</tr>
				<tr>
					<td class="width-15 active"><label class="pull-right">标签：</label></td>
					<td class="width-35">
						<form:textarea path="labels" htmlEscape="false" rows="4"    class="form-control "/>
						<span class="help-inline">多个标签使用【;】隔开</span>
					</td>
					<td class="width-15 active"><label class="pull-right">排序：</label></td>
					<td class="width-35">
						<form:input path="sort" htmlEscape="false"  class="form-control "/>
					</td>
				</tr>
				<tr>
					<td class="active"><label class="pull-right">出行地址：</label></td>
					<td class="width-35">
						<form:input path="city" htmlEscape="false"  class="form-control "/>
						<span class="help-inline">如：成都 | 嘉峪关长城 | 伦敦 | 迪士尼乐园</span>
					</td>
				</tr>
				<tr>
					<td class="active"><label class="pull-right">内容：</label></td>
					<td class="active" colspan="3"></td>
				</tr>
		   		<tr>
					<td colspan="4">
						<div id="editormd">
							<textarea>${fns:unescapeHtml(note.contentsmd)}</textarea>
						</div>
					</td>
				</tr>
		 	</tbody>
		</table>
	</form:form>
</body>
</html>
